﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using APP.HELPER.Utils.Language;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using Dictionary.ERP;
using DevExpress.XtraEditors.Controls;
using Common.Class;
using System.Data;
using Common;
using System.Drawing;
using Extra.ERP;
using Security.ERP;

namespace Dictionary
{
    public class xucDepartmentAdd : xucBaseAdd
    {
        public delegate void SuccessEventHander(object sender, HRM_DEPARTMENT item);

        private readonly IContainer components = null;
        private GridLookUpEdit glkBranch;
        private GridColumn gridColumn1;
        private GridColumn gridColumn2;
        private GridColumn gridColumn3;
        private GridColumn gridColumn4;
        private GridView gridLookUpEdit1View;
        private LabelControl lcBranch;
        private LabelControl lcDescription;
        private LabelControl lcPhone;
        private LabelControl lcQuantity;
        private TextEdit txtDescription;
        private CalcEdit txtFactQuantity;
        private TextEdit txtPhone;
        private CalcEdit txtQuantity;

        public xucDepartmentAdd()
        {
            InitializeComponent();
            InitMultiLanguages();
            InitData();
            txtQuantity.Text = "0";
            txtFactQuantity.Text = "0";
        }

        public event SuccessEventHander Success;

        protected override void Add()
        {
            base.Add();
            base.txtID.Text = new HRM_DEPARTMENT().NewID();
            base.txtNAME.Focus();
        }

        public void Clear()
        {
            base.txtID.Text = "";
            base.txtNAME.Text = "";
            txtPhone.Text = "";
            txtQuantity.Text = "0";
            txtFactQuantity.Text = "0";
            txtDescription.Text = "";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void glk_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            xfmBranchAdd.AddedEventHander hander = null;
            if (e.Button.Kind == ButtonPredefines.Plus)
            {
                var edit = sender as GridLookUpEdit;
                string name = edit.Name;
                if ((name != null) && (name == "glkBranch"))
                {
                    var add = new xfmBranchAdd(Actions.Add);
                    if (hander == null)
                    {
                        hander = (s, i) => new HRM_BRANCH().AddGridLookupEdit(glkBranch);
                    }
                    add.Added += hander;
                    add.ShowDialog();
                }
            }
        }

        protected override void Init()
        {
        }

        private void InitData()
        {
            DataTable list = new HRM_BRANCH().GetList();
            glkBranch.Properties.DataSource = list;
        }

        private void InitializeComponent()
        {
            var manager = new ComponentResourceManager(typeof (xucDepartmentAdd));
            txtDescription = new TextEdit();
            lcDescription = new LabelControl();
            lcQuantity = new LabelControl();
            txtQuantity = new CalcEdit();
            txtPhone = new TextEdit();
            lcPhone = new LabelControl();
            lcBranch = new LabelControl();
            glkBranch = new GridLookUpEdit();
            gridLookUpEdit1View = new GridView();
            gridColumn3 = new GridColumn();
            gridColumn4 = new GridColumn();
            gridColumn1 = new GridColumn();
            gridColumn2 = new GridColumn();
            txtFactQuantity = new CalcEdit();
            base.gcInfo.BeginInit();
            base.gcInfo.SuspendLayout();
            base.txtNAME.Properties.BeginInit();
            base.txtID.Properties.BeginInit();
            base.imageCollection2.BeginInit();
            ((ISupportInitialize) base.Err).BeginInit();
            txtDescription.Properties.BeginInit();
            txtQuantity.Properties.BeginInit();
            txtPhone.Properties.BeginInit();
            glkBranch.Properties.BeginInit();
            gridLookUpEdit1View.BeginInit();
            txtFactQuantity.Properties.BeginInit();
            base.SuspendLayout();
            base.gcInfo.Controls.Add(txtFactQuantity);
            base.gcInfo.Controls.Add(glkBranch);
            base.gcInfo.Controls.Add(lcBranch);
            base.gcInfo.Controls.Add(txtPhone);
            base.gcInfo.Controls.Add(lcPhone);
            base.gcInfo.Controls.Add(lcQuantity);
            base.gcInfo.Controls.Add(txtQuantity);
            base.gcInfo.Controls.Add(txtDescription);
            base.gcInfo.Controls.Add(lcDescription);
            base.gcInfo.Size = new Size(0x178, 0xa3);
            base.gcInfo.TabIndex = 0;
            base.gcInfo.Controls.SetChildIndex(base.txtNAME, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtID, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblName, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblId, 0);
            base.gcInfo.Controls.SetChildIndex(lcDescription, 0);
            base.gcInfo.Controls.SetChildIndex(txtDescription, 0);
            base.gcInfo.Controls.SetChildIndex(txtQuantity, 0);
            base.gcInfo.Controls.SetChildIndex(lcQuantity, 0);
            base.gcInfo.Controls.SetChildIndex(lcPhone, 0);
            base.gcInfo.Controls.SetChildIndex(txtPhone, 0);
            base.gcInfo.Controls.SetChildIndex(lcBranch, 0);
            base.gcInfo.Controls.SetChildIndex(glkBranch, 0);
            base.gcInfo.Controls.SetChildIndex(txtFactQuantity, 0);
            base.txtNAME.Location = new Point(0x5e, 0x34);
            base.txtNAME.Size = new Size(0x112, 20);
            base.txtNAME.TabIndex = 6;
            base.lblName.Location = new Point(6, 0x39);
            base.lblName.Size = new Size(0x4c, 13);
            base.lblName.TabIndex = 5;
            base.lblName.Text = "T\x00ean ph\x00f2ng ban:";
            base.txtID.Location = new Point(0x5e, 0x1a);
            base.txtID.Size = new Size(0x62, 20);
            base.lblId.Size = new Size(0x48, 14);
            base.lblId.Text = "M\x00e3 ph\x00f2ng ban:";
            base.imageCollection2.ImageStream =
                (ImageCollectionStreamer) manager.GetObject("imageCollection2.ImageStream");
            base.btnSave.Location = new Point(0x51, 0xaf);
            base.btnSave.TabIndex = 1;
            base.btnCancel.Location = new Point(0x110, 0xaf);
            base.btnCancel.TabIndex = 3;
            base.btnSaveNew.Location = new Point(0xb3, 0xaf);
            base.btnSaveNew.TabIndex = 2;
            txtDescription.Location = new Point(0x5e, 0x86);
            txtDescription.Name = "txtDescription";
            txtDescription.Size = new Size(0x112, 20);
            txtDescription.TabIndex = 12;
            lcDescription.Location = new Point(5, 0x89);
            lcDescription.Name = "lcDescription";
            lcDescription.Size = new Size(0x27, 13);
            lcDescription.TabIndex = 11;
            lcDescription.Text = "Ghi ch\x00fa:";
            lcQuantity.Location = new Point(0xc5, 0x1d);
            lcQuantity.Name = "lcQuantity";
            lcQuantity.Size = new Size(0x49, 13);
            lcQuantity.TabIndex = 2;
            lcQuantity.Text = "Số lượng (Σ/T):";
            txtQuantity.Location = new Point(0x111, 0x1a);
            txtQuantity.Name = "txtQuantity";
            txtQuantity.Properties.Buttons.AddRange(new EditorButton[] {new EditorButton(ButtonPredefines.Combo)});
            txtQuantity.Properties.ReadOnly = true;
            txtQuantity.Size = new Size(0x2e, 20);
            txtQuantity.TabIndex = 3;
            txtPhone.Location = new Point(0x5e, 0x6b);
            txtPhone.Name = "txtPhone";
            txtPhone.Size = new Size(0x112, 20);
            txtPhone.TabIndex = 10;
            lcPhone.Location = new Point(5, 110);
            lcPhone.Name = "lcPhone";
            lcPhone.Size = new Size(0x42, 13);
            lcPhone.TabIndex = 9;
            lcPhone.Text = "Số điện thoại:";
            lcBranch.Location = new Point(6, 0x53);
            lcBranch.Name = "lcBranch";
            lcBranch.Size = new Size(0x52, 13);
            lcBranch.TabIndex = 7;
            lcBranch.Text = "Thuộc chi nh\x00e1nh:";
            glkBranch.Location = new Point(0x5e, 0x51);
            glkBranch.Name = "glkBranch";
            glkBranch.Properties.Buttons.AddRange(new EditorButton[]
            {new EditorButton(ButtonPredefines.Combo), new EditorButton(ButtonPredefines.Plus)});
            glkBranch.Properties.DisplayMember = "BranchName";
            glkBranch.Properties.NullText = "[Chọn chi nh\x00e1nh]";
            glkBranch.Properties.ValueMember = "BranchCode";
            glkBranch.Properties.View = gridLookUpEdit1View;
            glkBranch.Size = new Size(0x112, 20);
            glkBranch.TabIndex = 8;
            glkBranch.ButtonClick += new ButtonPressedEventHandler(glk_ButtonClick);
            gridLookUpEdit1View.Columns.AddRange(new[] {gridColumn3, gridColumn4});
            gridLookUpEdit1View.FocusRectStyle = DrawFocusRectStyle.RowFocus;
            gridLookUpEdit1View.Name = "gridLookUpEdit1View";
            gridLookUpEdit1View.OptionsSelection.EnableAppearanceFocusedCell = false;
            gridLookUpEdit1View.OptionsView.ShowGroupPanel = false;
            gridColumn3.Caption = "M\x00e3 chi nh\x00e1nh";
            gridColumn3.FieldName = "BranchCode";
            gridColumn3.Name = "gridColumn3";
            gridColumn3.Visible = true;
            gridColumn3.VisibleIndex = 0;
            gridColumn4.Caption = "T\x00ean chi nh\x00e1nh";
            gridColumn4.FieldName = "BranchName";
            gridColumn4.Name = "gridColumn4";
            gridColumn4.Visible = true;
            gridColumn4.VisibleIndex = 1;
            gridColumn1.Caption = "M\x00e3 chi nh\x00e1nh";
            gridColumn1.FieldName = "BranchCode";
            gridColumn1.Name = "gridColumn1";
            gridColumn1.Visible = true;
            gridColumn1.VisibleIndex = 0;
            gridColumn2.Caption = "T\x00ean chi nh\x00e1nh";
            gridColumn2.FieldName = "BranchName";
            gridColumn2.Name = "gridColumn2";
            gridColumn2.Visible = true;
            gridColumn2.VisibleIndex = 1;
            txtFactQuantity.Location = new Point(0x143, 0x1a);
            txtFactQuantity.Name = "txtFactQuantity";
            txtFactQuantity.Properties.Buttons.AddRange(new EditorButton[] {new EditorButton(ButtonPredefines.Combo)});
            txtFactQuantity.Properties.ReadOnly = true;
            txtFactQuantity.Size = new Size(0x2d, 20);
            txtFactQuantity.TabIndex = 4;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Name = "xucDepartmentAdd";
            base.Size = new Size(0x179, 0xd5);
            base.gcInfo.EndInit();
            base.gcInfo.ResumeLayout(false);
            base.gcInfo.PerformLayout();
            base.txtNAME.Properties.EndInit();
            base.txtID.Properties.EndInit();
            base.imageCollection2.EndInit();
            ((ISupportInitialize) base.Err).EndInit();
            txtDescription.Properties.EndInit();
            txtQuantity.Properties.EndInit();
            txtPhone.Properties.EndInit();
            glkBranch.Properties.EndInit();
            gridLookUpEdit1View.EndInit();
            txtFactQuantity.Properties.EndInit();
            base.ResumeLayout(false);
        }

        private void InitMultiLanguages()
        {
            base.lblId.Text = MultiLanguages.GetString("tbl_DepartmentAdd", "DepartmentCode", base.lblId.Text);
            base.lblName.Text = MultiLanguages.GetString("tbl_DepartmentAdd", "DepartmentName", base.lblName.Text);
            lcQuantity.Text = MultiLanguages.GetString("tbl_DepartmentAdd", "Quantity", lcQuantity.Text);
            lcBranch.Text = MultiLanguages.GetString("tbl_DepartmentAdd", "BranchCode", lcBranch.Text);
            lcPhone.Text = MultiLanguages.GetString("tbl_DepartmentAdd", "Phone", lcPhone.Text);
            lcDescription.Text = MultiLanguages.GetString("tbl_DepartmentAdd", "Description", lcDescription.Text);
        }

        private void RaiseSuccessEventHander(HRM_DEPARTMENT item)
        {
            if (Success != null)
            {
                Success(this, item);
            }
        }

        public void SetBranchCode(string BranchCode)
        {
            glkBranch.EditValue = BranchCode;
        }

        public void SetData(HRM_DEPARTMENT item)
        {
            base.txtID.Text = item.DepartmentCode;
            SYS_LOG.Insert("Danh S\x00e1ch Ph\x00f2ng Ban", "Xem", base.txtID.Text);
            if (base.m_Status == Actions.Update)
            {
                base.txtID.Properties.ReadOnly = true;
            }
            glkBranch.EditValue = item.BranchCode;
            base.txtNAME.Text = item.DepartmentName;
            txtPhone.Text = item.Phone;
            txtQuantity.Text = item.Quantity.ToString();
            txtFactQuantity.Text = item.FactQuantity.ToString();
            txtDescription.Text = item.Description;
        }

        protected override void txtID_EditValueChanged(object sender, EventArgs e)
        {
            var control = (TextEdit) sender;
            if (control.ErrorText != string.Empty)
            {
                base.Err.SetError(control, string.Empty);
            }
            var hrm_branch = new HRM_BRANCH();
            var hrm_department = new HRM_DEPARTMENT();
            var hrm_group = new HRM_GROUP();
            if ((base.m_Status == Actions.Add) &&
                ((hrm_branch.Exist(control.Text) || hrm_department.Exist(control.Text)) || hrm_group.Exist(control.Text)))
            {
                base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                control.Focus();
            }
        }

        protected override void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            var control = (TextEdit) sender;
            if ((e.KeyCode == Keys.Enter) | (e.KeyCode == Keys.Tab))
            {
                var hrm_branch = new HRM_BRANCH();
                var hrm_department = new HRM_DEPARTMENT();
                var hrm_group = new HRM_GROUP();
                if ((base.m_Status == Actions.Add) &&
                    ((hrm_branch.Exist(control.Text) || hrm_department.Exist(control.Text)) ||
                     hrm_group.Exist(control.Text)))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override string uc_Change()
        {
            return string.Empty;
        }

        protected override string uc_Delete()
        {
            var item = new HRM_DEPARTMENT
            {
                DepartmentCode = base.txtID.Text
            };
            string str = item.Delete();
            if (str == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            return str;
        }

        protected override string uc_Save()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiDepartment") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowAdd)
            {
                MyRule.Notify();
                return "";
            }
            if (MyLogin.Level > 1)
            {
                XtraMessageBox.Show("Cấp độ quản l\x00fd của bạn kh\x00f4ng được ph\x00e9p th\x00eam ph\x00f2ng ban!",
                    "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return "";
            }
            if (glkBranch.EditValue == null)
            {
                XtraMessageBox.Show("Vui l\x00f2ng chọn 1 chi nh\x00e1nh!", "Cảnh B\x00e1o");
                return "";
            }
            SYS_LOG.Insert("Danh S\x00e1ch Ph\x00f2ng Ban", "Th\x00eam", base.txtID.Text);
            base.SetWaitDialogCaption("Đang lưu dữ liệu...");
            Cursor.Current = Cursors.WaitCursor;
            var item = new HRM_DEPARTMENT(base.txtID.Text, glkBranch.EditValue.ToString(), base.txtNAME.Text,
                txtPhone.Text, int.Parse(txtQuantity.Text), int.Parse(txtFactQuantity.Text), txtDescription.Text);
            string text = item.Insert();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            Cursor.Current = Cursors.Default;
            this.DoHide();
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return text;
        }

        protected override string uc_Update()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiDepartment") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowEdit)
            {
                MyRule.Notify();
                return "";
            }
            SYS_LOG.Insert("Danh S\x00e1ch Ph\x00f2ng Ban", "Cập Nhật", base.txtID.Text);
            base.SetWaitDialogCaption("Đang cập nhật dữ liệu...");
            var item = new HRM_DEPARTMENT(base.txtID.Text, glkBranch.EditValue.ToString(), base.txtNAME.Text,
                txtPhone.Text, int.Parse(txtQuantity.Text), int.Parse(txtFactQuantity.Text), txtDescription.Text);
            string text = item.Update();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.DoHide();
            return text;
        }
    }
}